package com.xbts.screen.monitor.domain.request;

import com.alibaba.fastjson.JSON;
import com.dahuatech.icc.util.SignUtil;
import com.xbts.screen.monitor.domain.response.GetPublicKeyResponse;
import com.xbts.screen.monitor.domain.token.OauthConfigBaseInfo;
import com.xbts.screen.monitor.domain.util.HttpURLConnectionUtil;
import com.xbts.screen.monitor.domain.util.HttpsURLConnectionUtil;
import com.xbts.screen.monitor.domain.util.UrlUtil;
import com.xbts.screen.monitor.domain.constant.MonitorConstant;
import com.xbts.screen.monitor.domain.response.GetTokenResponse;
import com.xbts.screen.monitor.domain.token.OauthConfigUserPwdInfo;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取token请求类
 */
@Data
public class GetTokenRequest extends BaseRequest<GetTokenResponse> {
    //授权类型
    private String grant_type;
    //用户名
    private String username;
    //通过RSA加密密码,详见获取公钥接口
    private String password;
    //客户端id，由鉴权中心下发
    private String client_id;
    //客户端secret，由鉴权中心下发
    private String client_secret;
    //公钥，由鉴权中心下发
    private String public_key;
    //默认关闭验证码校验，即无需关注verifyCodeFlag和code参数
    private Integer verifyCodeFlag = 0;
    private String code;

    public GetTokenRequest(){}

    public GetTokenRequest(OauthConfigBaseInfo oauthConfigBaseInfo){

        super(oauthConfigBaseInfo, MonitorConstant.HTTP_POST, oauthConfigBaseInfo.getHttpConfigInfo().getPrefixUrl() + MonitorConstant.GET_TOKEN, false);
        OauthConfigUserPwdInfo oauthConfigUserPwdInfo = (OauthConfigUserPwdInfo)oauthConfigBaseInfo;
        this.grant_type = oauthConfigUserPwdInfo.getGrantType().name();
        this.username = oauthConfigUserPwdInfo.getUsername();
        this.client_id = oauthConfigUserPwdInfo.getClientId();
        this.client_secret = oauthConfigUserPwdInfo.getClientSecret();
        GetPublicKeyRequest request = new GetPublicKeyRequest(oauthConfigBaseInfo);
        GetPublicKeyResponse getPublicKeyResponse = request.doAction();
        this.public_key = getPublicKeyResponse.getData().getPublicKey();
        String enPassword = SignUtil.encryptRSA(oauthConfigUserPwdInfo.getPassword(), this.public_key);
        this.password = enPassword;
    }

    public GetTokenResponse doAction(){
        //重置url信息，替换版本号
        this.setUrl(UrlUtil.dealVersion(this.getUrl()));
        //设置请求参数
        setBody();
        //发送请求
        String res = isEnableHttpTest() ? HttpURLConnectionUtil.doPost(this.getUrl(), this.getBody(), this.getHeader()) : HttpsURLConnectionUtil.doPost(this.getUrl(), this.getBody(), this.getHeader());
        //获取响应
        GetTokenResponse getTokenResponse = JSON.parseObject(res, GetTokenResponse.class);
        return getTokenResponse;
    }

    /**
     * 设置请求参数
     * **/
    public void setBody(){
        //设置请求参数
        Map<String, String> paramsMap = new HashMap();
        paramsMap.put("grant_type", this.grant_type);
        paramsMap.put("username", this.username);
        paramsMap.put("password", this.password);
        paramsMap.put("client_id", this.client_id);
        paramsMap.put("client_secret", this.client_secret);
        paramsMap.put("public_key", this.public_key);
        paramsMap.put("verifyCodeFlag", String.valueOf(this.verifyCodeFlag));
        this.setBody(JSON.toJSONString(paramsMap));
    }

    public boolean isEnableHttpTest(){
        return super.getOauthConfigBaseInfo().getHttpConfigInfo().isEnableHttpTest();
    }
}
